
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>总仓订货单</title>
	<link href="../../rs/css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
	<link href="../../rs/css/font-awesome.min93e3.css?v=4.4.0" rel="stylesheet">
	<link href="../../rs/plugins/bootstrap_tab/css/bootstrap-table.css" rel="stylesheet">
	<link href="../../rs/css/animate.min.css" rel="stylesheet">
	<link href="../../rs/css/style.min862f.css?v=4.1.0" rel="stylesheet">
	<link href="../../rs/plugins/datapicker/datepicker3.css" rel="stylesheet">
	<link href="../../rs/plugins/bootstrap-editable/css/bootstrap-editable.css" rel="stylesheet">
  
  <?php
    require_once('login.php');
   ?>
  <style>
         #toolbar1 {
             padding: 0px 15px;
             float: right;
         }
         .btn-group-sm > .btn, .btn-sm {
             padding: 5px 12px;
         }

         .editable-click, a.editable-click, a.editable-click:hover {
             border-bottom: 0px;
         }
         .fixed-table-pagination .page-list{
             display: none;
         }
     </style>


</head>

<body class="gray-bg">
  <div class="wrapper wrapper-content animated fadeInRight">
    <div class="ibox float-e-margins" style="    background: #fff;padding: 20px;">
      <div class="row">
        <div class="col-sm-4 m-b-xs">
            <div class="form-group" id="data_1">
                <div class="input-group date">
                    <span class="input-group-addon">订单日期</span>
                    <input type="text" class="form-control" placeholder="请选择单据日期" name="djrq" id="djrq" readonly="readonly">
                </div>
            </div>
        </div>
        <div class="col-sm-4 m-b-xs">
            <div class="form-group" id="data_2">
                <div class="input-group date">
                    <span class="input-group-addon">预计到货日期</span>
                    <input type="text" class="form-control" placeholder="请选择单据日期" name="yjrq" id="yjrq"  readonly="readonly">
                </div>
            </div>
        </div>
        <div class="col-sm-4 m-b-xs">
          <div id="toolbar1">

              <button  onclick="cancelall()" style="float:left;margin-left:5px" class="btn" id="del">
                <i class="glyphicon glyphicon-remove"></i> 删除
              </button>
              <button onclick="save_do()" style="float:left;margin-left:5px" class="btn btn-success">
                  <i class="glyphicon glyphicon-ok"></i> 保存
              </button>
              <button onclick="save_sh()" style="float:left;margin-left:5px" class="btn btn-danger">
                  <i class="glyphicon glyphicon-zoom-in"></i> 审核
              </button>
          </div>
        </div>
      </div>

      <div class="row">
        <div class="col-sm-4 m-b-xs">
            <div class="form-group draggable">
                <div class="input-group">
                        <span class="input-group-btn">
                            <button style="height:34px;background-color: #FFF;border: 1px solid #e5e6e7;font-size: 14px;margin-left:-1px" type="button" class="btn btn-sm ">订 单</button>
                        </span>
                    <select class="form-control" id="selectorder">
                        <option value="-1">新单</option>
                    </select>
                </div>
            </div>
        </div>
        <div class="col-sm-4">
            <div class="input-group ">
                        <span class="input-group-btn">
                            <button style="height:34px;background-color: #FFF;border: 1px solid #e5e6e7;font-size: 14px;margin-left:-1px" type="button" class="btn btn-sm ">仓库</button>
                        </span>
                <select class="form-control" id="m_ck">
                </select>
            </div>
        </div>

          <div class="col-sm-4">
              <div class="input-group ">
                          <span class="input-group-btn">
                              <button style="height:34px;background-color: #FFF;border: 1px solid #e5e6e7;font-size: 14px" type="button" class="btn btn-sm ">客户</button>
                          </span>
                  <select class="form-control" id="m_kh">
                  </select>
              </div>
          </div>
      </div>


      <div class="row">
          <div class="col-sm-12">
              <div class="input-group">
                  <div class="input-group-btn">
                      <button style="height:34px" type="button" class="btn btn-white dropdown-toggle " data-toggle="dropdown">
                          订单备注
                      </button>
                  </div>
                  <input type="text" class="form-control" id="ddbz">
              </div>
          </div>
      </div>
    </div>

      <!-- Panel Other -->

      <div class="ibox float-e-margins">
          <div class="ibox-content">
              <div class="row">
                  <div>
                      <div class="col-sm-4">
                        <button style="float:left;  margin-right:5px;margin-bottom: 20px;height:34px;" id="remove" class="btn btn-danger" disabled>
                            <i class="glyphicon glyphicon-trash" style="font-size:12px"></i>
                        </button>
                      </div>
                      <div class="col-sm-4 m-b-xs" style="float: right;">
                          <div class="input-group">
                            <div class="input-group-btn">
                                <button style="height:34px;margin-left:-1px;" type="button" class="btn btn-white dropdown-toggle" onclick="open_modal()">
                                    产品资料
                                </button>
                                <ul class="dropdown-menu dropdown-menu-right" role="menu"></ul>
                            </div>
                              <input type="text" class="form-control" id="searchtxt" onkeydown="_key()">
                              <div class="input-group-btn">
                                  <button style="height:34px" type="button" class="btn btn-white dropdown-toggle" onclick="open_modal()">
                                      搜索
                                  </button>
                                  <ul class="dropdown-menu dropdown-menu-right" role="menu"></ul>
                              </div>
                          </div>
                      </div>
                  </div>
              </div>
              <table id="table"></table>
              <!-- End Example Card View -->
          </div>
      </div>
  </div>


<script src="../../rs/js/jquery.min.js"></script>
<script src="../../rs/js/bootstrap.js"></script>
<script src="../../rs/plugins/bootstrap_tab/js/bootstrap-table.min.js"></script>
<script src="../../rs/plugins/bootstrap_tab/js/plugins/bootstrap-table/locale/bootstrap-table-zh-CN.min.js"></script>
<script src="../../rs/plugins/datapicker/bootstrap-datepicker.js"></script>
<script src="../../rs/js/jquery.cookie.js"></script>
<script src="../../rs/js/common_qj.js"></script>
<script src="../../rs/plugins/layer/layer.min.js"></script>
<script src="../../rs/plugins/bootstrap-editable/js/bootstrap-editable.js"></script>
<script src="../../rs/plugins/bootstrap-editable/js/bootstrap-table-editable.min.js"></script>


 <script type="text/javascript">
     var $table = $('#table');
     var $remove = $('#remove');
     var sign = URLencode($.cookie('sign'));

     $(function () {
         //日期选择插件
         $("#data_1 .input-group.date").datepicker({
             todayBtn: "linked",
             keyboardNavigation: !1,
             forceParse: !1,
             calendarWeeks: !0,
             autoclose: !0
         });
          $("#data_2 .input-group.date").datepicker({
             todayBtn: "linked",
             keyboardNavigation: !1,
             forceParse: !1,
             calendarWeeks: !0,
             autoclose: !0
         });

         var djrq = (new Date()).format('yyyy-MM-dd');
          
         $('#djrq').val(djrq);  //初始化时间
         $('#yjrq').val(djrq);  //初始化时间
         $('#table').bootstrapTable({
					 columns: [
 							{
 									field: 'state',
 									checkbox: true
 							},
 							{
 									field: 'prid',
 									title: '产品编号',
 									width:'200px',
 									align: 'left'
 							},
 							{
 									field: 'prmc',
 									align: 'left',
 									width:'300px',
 									title: '产品名称'
 							},
 							{
 									field: 'dj',
 									align: 'center',
 									title: '单价',
 									editable: true,
 									width:'100px',
 							},
 							 
 							{
 									field: 'sl',
 									align: 'right',
 									title: '数量',
 									width:'100px',
 									editable: true
 							},
 							{
 									field: 'bz',
 									align: 'center',
 									title: '备注',
 									editable: true
 							}
 					],
             advancedSearch: true,
             idTable: "advancedTable",
             shadeClose: true,
             toggle: "table",

             //当修改数值后执行的保存操作
             onEditableSave: function (field, row, oldValue, $el) {
                 if (field == 'sl') {
                     if (isNaN(row.sl)) {
                         row.sl = oldValue;
                     }if(row.sl==""){
								 row.sl = oldValue;
										 }else {
                         row.sl=parseFloat(row.sl).toFixed(2);
                     }
                 }
                 if (field == 'dj') {
                     if (isNaN(row.dj)) {
                         row.dj = oldValue;
                     }if(row.dj==""){
											 row.dj = oldValue;
										 }else {
                         row.dj=parseFloat(row.dj).toFixed(2);
                     }
                 }
                 $('#table').bootstrapTable('load', dtn);
                 banhao();
                 return true;
             }
         });
        f_sheard();

				<!-- 删除操作 -->
        $table.on('check.bs.table uncheck.bs.table check-all.bs.table uncheck-all.bs.table', function () {
            $remove.prop('disabled', !$table.bootstrapTable('getSelections').length);
        });
        $remove.click(function () {
            layer.confirm('确定要删除这个产品吗？', {
                title: [''],
                btn: ['确定', '取消'] //按钮
            	}, function (index) {
                var prids = $.map($table.bootstrapTable('getSelections'), function (row) {
                    return row.prid;
                });
								layer.close(index);
                $table.bootstrapTable('remove', {
               			field: 'prid',
               			values: prids
			           });



            }, function (index) {
                layer.close(index);
            });
        });
        <!-- 删除操作 -->

         //隐藏按钮
         var cz1 = $("#selectorder  option:selected").text();
         if(cz1=='新单'){
						 $('#del').attr("disabled",true);
         }
     });

     var slpr = {};  //定义产品资料

//获取仓库、客户资料、产品资料、订单 信息
     function f_sheard() {
         layer.load(2);
         //给仓库做循环
         $.ajax({
             type: "post",
             url:G_url+'sysg/see',
             data: 'sign=' + sign,
             dataType: "json",
             success: function (result) {
                 layer.closeAll('loading');
                 var ff = result.data;
                 var html1 = '';
                 $.each(ff, function (a, b) {//直接取出json的prolist做each循环.
                     html1 += "<option value='" + b.ShopGroupID + "'>" + b.ShopGroupName + "</option>";//拼接字符串.
                 });
                 $("#m_ck").append(html1);
                 //$("#mdbh1").append(html1);
             },
             error: function (XMLHttpRequest, textStatus, errorThrown) {
             }
         });
         //客户资料
         $.ajax({
             type: "post",
             url:G_url+'slcu/see',
             data: 'sign=' + sign,
             dataType: "json",
             success: function (result) {
                 layer.closeAll('loading');
                 var ff = result.data;
                 var html1 = '';
                 $.each(ff, function (a, b) {//直接取出json的prolist做each循环.
                     html1 += "<option value='" + b.cuid + "'>" + b.cuname1 + "</option>";//拼接字符串.
                 });
                 $("#m_kh").append(html1);
                 //$("#mdbh1").append(html1);
             },
             error: function (XMLHttpRequest, textStatus, errorThrown) {
             }
         });


         //产品资料
         $.ajax({
             type: "post",
             url:G_url+'slpr/see',
             data: 'sign=' + sign,
             dataType: "json",
             success: function (result) {
                 layer.closeAll('loading');
                 if (result.ok) {
     					     slpr =result.data;
     						} else {
     							layer.msg(result.msg);
     						}
             },
             error: function (XMLHttpRequest, textStatus, errorThrown) {
             }
         });


				 //订单order
				 $.ajax({
						 type: "post",
						 url:G_url+'storo/news/'+'?sign='+sign,
						 data: 'sign=' + sign,
						 dataType: "json",
						 success: function (result) {
							 layer.closeAll('loading');
							 var ff = result.data.dt.dh;
							 var html1 = '';
							 $.each(ff, function (a, b) {//直接取出json的prolist做each循环.
									 html1 += "<option value='" + b.djbh + "'>" + b.djbh + "</option>";//拼接字符串.
							 });

							 $("#selectorder").append(html1);
						 },
						 error: function (XMLHttpRequest, textStatus, errorThrown) {
						 }
				 });

     }


     //给button添加回车事件
     function _key() {
         if(event.keyCode ==13){
             open_modal();
             $("#searchtxt").blur();
         }
     }
      function open_modal() {
         var s = $('#searchtxt').val();
				 //产品资料
				 var ck_name = $("#m_ck  option:selected").val();
                 var khbh=$("#m_kh option:selected").val();
				 $.ajax({
						 type: "post",
						 url:G_url+'stino/seeslpr/'+ck_name+ "?sign="+sign,
						 data: 'khbh=' + khbh+"&djlx=zd",
						 dataType: "json",
						 success: function (result) {
								 layer.closeAll('loading');
								 if (result.ok) {
									slpr=result.data;
								$('#table1').bootstrapTable('destroy');//删除table表格
								$('#table1').bootstrapTable({//重新建立表格
                                            columns: [
                                            {
                                                field: 'prid',
                                                title: '商品编号'
                                            },
                                            {
                                                field: 'prname1',
                                                title: '商品名称'
                                            },
                                            {
                                                field: 'prpricein',
                                                title: '单价'
                                            },
                                            {
                                                field: 'insert',
                                                title: '添加',
                                                align: 'center',
                                                formatter: insertFormatter
                                            }
                                        ],
									shadeClose: true,
									searchText: s,
									data: slpr,
									toggle: "table",
							 
									search: true
							});

								} else {
									layer.msg(result.msg);
								}
						 },
						 error: function (XMLHttpRequest, textStatus, errorThrown) {
						 }
				 });

         //页面层
         layer.open({
             title: ['添加商品'],
             type: 1,
             offset: 'rb',//top顶部偏移
             area: ['60%', '100%'], //宽高
             shadeClose: true,
             content: '<div style="padding: 20px 30px 0 30px "><table id="table1"></table></div>'
         });

     }

     
     function insert_do(a) {
         var r=slpr[a];
         var lno = 0;
         var sfcz = false;
         for (var i = 0, l = dtn.length; i < l; i++) {
             if (dtn[i].prid == r.prid) {
                 lno = i;
                 sfcz = true;
                 break
             }
         }

         if (sfcz) {
             layer.open({
                 content: '数据存已在是否累加?',
                 title: [''],
                 btn: ['累加', '新增'], //按钮
                 yes: function (index) {
                     //累加
                     dtn[lno].sl++;
                     $table.bootstrapTable('load', dtn);
                     banhao();
                     layer.close(index);
                 },
                 btn2: function (index) {
                    //新增
                    var n = {};
                    n.prid = r.prid;
                    n.prmc = r.prname1;
                    n.dj = (r.ckdj==null ) ? r.prpricein : r.ckdj;
                    n.sl = 1;
                    n.rid= 0;
                    n.bz=" ";
                    n.si=1;
                    dtn.push(n);
                    $table.bootstrapTable('load', dtn);
                    banhao();
                    layer.close(index);
                },
                 cancle: function (index) {
                     layer.close(index);
                 }
             });
         } else {
             //新增
						 var n = {};
             n.prid = r.prid;
             n.prmc = r.prname1;
              n.dj = (r.ckdj==null ) ? r.prpricein : r.ckdj;
             n.sl = 1;
			 n.rid= 0;
             n.bz=" ";
             n.si=1;
             dtn.push(n);
             $table.bootstrapTable('load', dtn);
             banhao();
         }

     }

     //使用函数将调用的那一行的样式写出
     function insertFormatter(value, row,index) {
         //return "<button type='button' class='btn btn-info' onclick='insert_do(" + JSON.stringify(row) +  ")'>添加</button> ";   
         return '<button type="button" class="btn btn-info" onclick="insert_do('+ index +')">添加</button> ';   
     }
 

     var kk=-1;
    


		$("#selectorder").change(function () {

						var cz = $("#selectorder  option:selected").text();
						var cv = $("#selectorder  option:selected").val();
						kk=cv;
						if(cz=='新单'){
							 //  $("#del").hide();
							 $('#del').attr("disabled",true);
								location.reload();
						}else {
								//$("#del").show();
								$('#del').attr("disabled",false);
						}
						f_detail(cz);


		});





     var dtn = [];
     var sign1=$.cookie('sign');
     function f_detail(djbh) {
         $.ajax({
             type: "post",
             url: G_url+'storo/load/'+djbh,
             data:  'sign=' + sign,
             dataType: "json",
             success: function (result) {
                 layer.closeAll();
                 if (!result.ok) {
                     layer.msg(result.msg);
                 }
                 dtn = result.detail;
 

					//根据获得的数据重新写入 时间  仓库  客户资料
					//时间
						$("#djrq").val(result.order.djrq);
					//仓库
						var ck_val=result.order.ckbh;
						$("#m_ck").find("option[value="+ck_val+"]").prop("selected",true);
					//客户资料
 						var kh_val=result.order.khbh;
 						$("#m_kh").find("option[value="+kh_val+"]").prop("selected",true);

					//订单备注赋值
                     if($.trim(result.order.xtbz)==""){
                         $("#ddbz").val(result.order.bz);
                    }else if($.trim(result.order.xtbz)!=""){
                     $("#ddbz").val(result.order.xtbz+"/"+result.order.bz);
                    }
                                  
                  $('#table').bootstrapTable('load', dtn);
                  banhao();
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                }
         });
     }

     function cancelall(){
         layer.confirm('确定删除这个订单吗？', {
             title: [''],
             btn: ['确定', '取消'] //按钮
         }, function (index) {
             layer.msg('数据删除中...', {icon: 16, time: 200000, shade: [0.1, 'black']});
             var djbh_del = $("#selectorder  option:selected").text();
             $.ajax({
                 type: "post",
                 url: G_url+'storo/del/'+djbh_del,
                 data:'sign=' + sign,
                 dataType: "json",
                 success: function (result) {
                     layer.closeAll();
										 location.reload();
                 },
                 error: function (XMLHttpRequest, textStatus, errorThrown) {
                     layer.msg('数据删除失败', {time: 1000});
                 }
             });
             layer.close(index);
         }, function (index) {
             layer.close(index);
         });
     }

     var f_data={};
     function save_do(){

         var ckbh=$("#m_ck option:selected").val();
         var khbh=$("#m_kh option:selected").val();
             layer.confirm('确定要保存吗？', {
                 title: [''],
                 btn: ['确定', '取消'] //按钮
             }, function (index) {
                 layer.msg('数据保存中...', {icon: 16, time: 200000, shade: [0.1, 'black']});
                 var djrq=$('#djrq').val();
                 var yjrq=$('#yjrq').val();
                 var djbh=$("#selectorder  option:selected").text();
                 var bz=$('#ddbz').val();
                 var data=[{
                     "djrq":djrq,
                     "djbh":djbh,  //订单日期
                     "yjrq":yjrq,  //预计到货日期
                     "bz":bz,
                     "ckbh":ckbh,
                     "khbh":khbh,
                     djje:'0'
                 }];
                 var data1=$('#table').bootstrapTable('getData');
								 console.log(data1);
                 var save={};
                 save.order=data;
                 save.detail=data1;
                //save.sign=sign1;
                 $.ajax({
                     type: "post",
                     url: G_url+'storo/save/'+"?sign="+sign,
                     data:JSON.stringify(save),
                     dataType: "json",
                     success: function(res) {
                         if (res.ok){
                             layer.msg(res.msg);
                             f_data=res.detail;
                             //var ss = dtnheard.order;
                             if(djbh=="新单"){
                                 $("#selectorder").append( "<option value='"+ res.djbh+"' selected>" + res.djbh + "</option>");
                             }

                             //$("#del").show();
														 $('#del').attr("disabled",false);

                             $('#table').bootstrapTable('load', f_data);
                         } else {
                             layer.msg(res.msg);

                         }
                     },
                     error: function (XMLHttpRequest, textStatus, errorThrown) {
                         layer.msg('数据保存失败', {time: 1000});
                     }
                 });
                 layer.close(index);
             }, function (index) {
                 layer.close(index);
             });

     }

    

     function save_sh(){
			 var djbhsh=$("#selectorder  option:selected").text();
			 if(djbhsh=="新单"){
					layer.msg("请选择要审核的订单！");
					return;
			 }
         layer.confirm('确定提交审核吗？', {
             title: [''],
             btn: ['确定', '取消'] //按钮
         }, function (index) {
             //sh();
             layer.msg('数据提交中...', {icon: 16, time: 200000, shade: [0.1, 'black']});

             $.ajax({
                 async: false,
                 type: "post",
                 url: G_url+'storo/sh/'+djbhsh,
                 data:'sign=' + sign,
                 dataType: "json",
                 success: function(res) {
                     if (res.ok){
                         layer.msg(res.msg);
                         location.reload();
                     } else {
                         layer.msg(res.msg);

                     }
                 },
                 error: function (XMLHttpRequest, textStatus, errorThrown) {
                     layer.msg('审核失败！', {time: 1000});
                 }
             });
             layer.close(index);
         }, function (index) {
             layer.close(index);
         });
     }


     //排序功能
function banhao(){
	var len = $('#table tbody tr').length;
	for(var i = 0;i<len;i++){
		$('table tbody tr:eq('+i+') td:nth-child(2)').text(i+1);
	}
}


 </script>
</body>
</html>
